home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / Fstream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-02  |  5.1 KB  |  179 lines

  1. /*  fstream.h -- class filebuf and fstream declarations
  2.  
  3.     Copyright 1991, 1993 by TauMetric Corporation      ALL RIGHTS RESERVED
  4.  
  5.     @(#)fstream.h    1.4  5/28/93 11:52:41
  6. */
  7. #ifndef _FSTREAM_H_
  8. #define _FSTREAM_H_
  9.  
  10. #ifndef __cplusplus
  11. #error  Use C++ compiler for fstream.h
  12. #endif
  13.  
  14. #include <iostream.h>
  15.  
  16. #pragma pack(__DEFALIGN)
  17. class  filebuf : public streambuf {
  18. public:
  19. static const int openprot;    // default file protection
  20. static const int sh_compat;
  21. static const int sh_none;
  22. static const int sh_read;
  23. static const int sh_write;
  24.  
  25. // options for setmode member function
  26. static  const int   binary;
  27. static  const int   text;
  28.  
  29.         // constructors, destructor
  30.         filebuf();    // make a closed filebuf
  31.         filebuf(int);    // make a filebuf attached to fd
  32.         filebuf(int, char*, int); // same, with specified buffer
  33.         ~filebuf();
  34.  
  35.     int    is_open();    // is the file open
  36.     int    fd();        // what is the file descriptor
  37.  
  38.     // open named file with mode and protection, attach to this filebuf
  39.     filebuf* open(const char*, int, int = filebuf::openprot);
  40.  
  41.     filebuf* close();    // flush and close file
  42.     int setmode(int = filebuf::text);
  43.     filebuf* attach(int);    // attach this filebuf to opened file descriptor
  44.     int     detach();    // detach from and return the file descriptor
  45.  
  46. /*
  47.  * These perform the streambuf functions on a filebuf
  48.  * Get and Put pointers are kept together
  49.  */
  50. virtual int    overflow(int = EOF);
  51. virtual int    underflow();
  52. virtual int    sync();
  53. virtual streampos  seekoff(streamoff, ios::seek_dir, int);
  54. virtual streambuf* setbuf(char*, int);
  55.  
  56. protected:
  57.     int    xfd;        // the file descriptor, EOF if closed
  58.     int    mode;        // the opened mode
  59.     short    opened;        // non-zero if file is open
  60.  
  61.     streampos last_seek;    // unused            ***
  62.     char*   in_start;    // unused            ***
  63.  
  64.     int    last_op();    // unused            ***
  65.     char    lahead[2];    // current input char if unbuffered ***
  66. };
  67. /*
  68.  * The data members marked with *** above are not documented in the AT&T
  69.  * release of streams, so we cannot guarantee compatibility with any
  70.  * other streams release in the use or values of these data members.
  71.  * If you can document any expected behavior of these data members, we
  72.  * will try to adjust our implementation accordingly.
  73.  */
  74. inline int    filebuf::is_open()    { return opened; }
  75. inline int    filebuf::fd()        { return xfd; }
  76.  
  77.  
  78. class fstreambase : virtual public ios {
  79. public:
  80.         fstreambase();
  81.         fstreambase(const char*, int, int = filebuf::openprot);
  82.         fstreambase(int);
  83.         fstreambase(int _f, char*, int);
  84.         ~fstreambase();
  85.  
  86.     void    open(const char*, int, int = filebuf::openprot);
  87.     void    attach(int);
  88.     void    close();
  89.     void    setbuf(char*, int);
  90.     filebuf* rdbuf();
  91.     int setmode(int mode = filebuf::text) {return rdbuf()->setmode(mode);}
  92.  
  93. protected:
  94.     void    verify(int);    // unimplemented    ***
  95.  
  96. private:
  97.     filebuf    buf;
  98. };
  99. /*
  100.  * The function member marked with *** above is not documented in the AT&T
  101.  * release of streams, so we cannot guarantee compatibility with any
  102.  * other streams release in its use.
  103.  * If you can document any expected behavior of this function member, we
  104.  * will try to adjust our implementation accordingly.
  105.  */
  106. inline filebuf* fstreambase::rdbuf() { return &buf; }
  107.  
  108.  
  109. class ifstream : public fstreambase, public istream {
  110. public:
  111.         ifstream();
  112.         ifstream(const char*, int=ios::in, int=filebuf::openprot);
  113.         ifstream(int);
  114.         ifstream(int, char*, int);
  115.         ~ifstream();
  116.  
  117.     filebuf* rdbuf();
  118.     
  119.     int is_open(){ return rdbuf()->is_open(); }
  120.     void    open(const char*, int=ios::in, int=filebuf::openprot);
  121.     void close();
  122. };
  123. inline filebuf* ifstream::rdbuf() { return fstreambase::rdbuf(); }
  124. inline void    ifstream::open(const char* _n, int _m, int _p)
  125.     { fstreambase::open(_n, _m | ios::in, _p); }
  126. inline void ifstream::close()
  127.    { fstreambase::close(); }
  128.  
  129.  
  130.  
  131. class ofstream : public fstreambase, public ostream {
  132. public:
  133.         ofstream();
  134.         ofstream(const char*, int=ios::out, int=filebuf::openprot);
  135.         ofstream(int);
  136.         ofstream(int _f, char*, int);
  137.         ~ofstream();
  138.  
  139.     filebuf* rdbuf();
  140.     
  141.     int is_open(){ return rdbuf()->is_open(); }
  142.     void open(const char*, int=ios::out, int=filebuf::openprot);
  143.     void close();
  144. };
  145. inline filebuf*    ofstream::rdbuf() { return fstreambase::rdbuf(); }
  146. inline void    ofstream::open(const char* name, int m, int prot)
  147.     { fstreambase::open(name, m | ios::out, prot); }
  148. inline void ofstream::close()
  149.   { fstreambase::close(); }
  150.  
  151.  
  152.  
  153. class fstream : public fstreambase, public iostream {
  154. public:
  155.         fstream();
  156.         fstream(const char*, int, int = filebuf::openprot);
  157.         fstream(int);
  158.         fstream(int _f, char*, int);
  159.         ~fstream();
  160.  
  161.     filebuf* rdbuf();
  162.     
  163.     int is_open(){ return rdbuf()->is_open(); }
  164.     void    open(const char *, int, int = filebuf::openprot);
  165.    void close();
  166. };
  167. inline filebuf* fstream::rdbuf() { return fstreambase::rdbuf(); }
  168. inline void    fstream::open(const char* name, int m, int prot)
  169.     { fstreambase::open(name, m, prot); }
  170. inline void fstream::close()
  171.   { fstreambase::close(); }
  172.  
  173. inline ios& binary(ios& st) {((filebuf*)st.rdbuf())->setmode(filebuf::binary); return st;}
  174. inline ios& text(ios& st) {((filebuf*)st.rdbuf())->setmode(filebuf::text); return st;}
  175.  
  176. #pragma pack()
  177.  
  178. #endif /* _FSTREAM_H_ */
  179.